#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年8月7日

@author: chenyongbing
'''
import sys,os,re
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]


import sys,os
current_dir = os.path.dirname(__file__)
import datetime

import logging
sys.path.append(os.path.join(current_dir,'../../'))
from base.PyMySQL import ConnectMySQL
from base.localConfig import baseLocalConfig
from base.time_method import myTimeMethod
from base.file_method import openTarFile
#from base.retail_datas import myRetailDatas,myRetail
import sys,datetime
zhimofi_host = baseLocalConfig.mysqlConfig['zhimofi']['host']
zhimofi_user = baseLocalConfig.mysqlConfig['zhimofi']['user']
zhimofi_passwd = baseLocalConfig.mysqlConfig['zhimofi']['passwd']
zhimofi_db = baseLocalConfig.mysqlConfig['zhimofi']['db']

myZhiMofi = ConnectMySQL(zhimofi_host,zhimofi_user,zhimofi_passwd,zhimofi_db)




#from stat_in_mofi import StatInMofi

class StatInMofi():
    
    def __init__(self,testing=False):
        self.testing = testing
    
    def analyze_flow_from_sta_log(self,line):
        device_buf = re.search('([\d]{4}-[\d]{2}-[\d]{2}\s*[\d]{1,2}:[\d]{2}:[\d]{2}) Sta.*?INFO :\s*([a-z0-9]+),buf\=(.*)$',line)
        rtime = device_buf.group(1)
        device = device_buf.group(2)
        buf = device_buf.group(3)
        try:
            buf_dict = eval(buf)
        except:
            logging.error('Buf %s Type Error.'%buf)
            return rtime , device , None
        if 'device_upflow' not in buf_dict.keys() or 'device_downflow' not in buf_dict.keys():
            logging.error('device flow not in %s'%buf)
            return rtime , device , None    
        upflow = buf_dict['device_upflow']
        downflow = buf_dict['device_downflow']
        return rtime,device, int(upflow) + int(downflow)
        
    
    
    def get_last_current_flow(self,group_devices,staPath='Udisk/mofidatas/',day=None):
        datas = {}
        if day == None:
            startTime = datetime.datetime.today().strftime('%Y-%m-%d %H')
        sta_file = staPath + '/sta.%s.txt'%day
        if not os.path.exists(sta_file):
            logging.error('Sta File %s Not Exists.'%sta_file)
            return datas
        #2015-06-08 18:34:31 Sta 9 [nioEventLoopGroup-5-4] INFO : 048d3861c7d8,buf={ "device_upflow": "31491682", "device_downflow": "71310327", "device_up_rate": 94, "device_down_rate": 203, "ntp_status": 1, "version": "V1.3.141231\n_2.1.5_20150520", "users": [ { "sta_name": "iPhone-2", "ssid": "Tangjiu", "sta_ip": "192.168.8.116", "sta_mac": "a0182884a6e9", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 39, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "15554", "sta_downflow": "41744" }, { "sta_name": "android-e975184", "ssid": "Tangjiu", "sta_ip": "192.168.8.149", "sta_mac": "88329b6b65fa", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 79, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "14757", "sta_downflow": "34698" }, { "sta_name": "applede-iPhone", "ssid": "Tangjiu", "sta_ip": "192.168.8.133", "sta_mac": "98f0ab648c74", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 99, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "54136", "sta_downflow": "238671" }, { "sta_name": "", "ssid": "Tangjiu", "sta_ip": "192.168.8.120", "sta_mac": "74e2f5568d8a", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 2192, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "1844713", "sta_downflow": "14981302" }, { "sta_name": "android-2f005a9", "ssid": "Tangjiu", "sta_ip": "192.168.8.193", "sta_mac": "3c9157ec50dd", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 4425, "sta_up_rate": 0, "sta_down_rate": 52, "sta_upflow": "1060806", "sta_downflow": "2855826" }, { "sta_name": "android-fb7719e", "ssid": "Tangjiu", "sta_ip": "192.168.8.196", "sta_mac": "18dc56b32a23", "sta_auth_type": 2, "sta_mode": 2, "sta_uptime": 6883, "sta_up_rate": 40, "sta_down_rate": 46, "sta_upflow": "3622895", "sta_downflow": "13890728" }, { "sta_name": "Pos0871", "ssid": "", "sta_ip": "192.168.8.166", "sta_mac": "00191700d40b", "sta_auth_type": 0, "sta_mode": 1, "sta_uptime": 59624, "sta_up_rate": 54, "sta_down_rate": 0, "sta_upflow": "12186735", "sta_downflow": "15587465" } ] }
        
        for line in open(sta_file).readlines():
            rtime , device , flow = self.analyze_flow_from_sta_log(line)
            if flow == None:
                continue
            #print rtime , device , flow     
            
            if not re.search('%s'%day,rtime):continue
            datas[device] = flow
        return datas
    def stat_flow_by_hour_history(self,group_devices,staPath='/Udisk/mofidatas/',startTime=None,endTime=None):
        if startTime == None:
            startTime = datetime.datetime.today().strftime('%Y-%m-%d %H')
        if endTime == None:
            endTime = datetime.datetime.today().strftime('%Y-%m-%d %H')
        
        startDay = startTime[:10]
        startHour = int(startTime[11:])
        endDay = endTime[:10]
        endHour = int(endTime[11:])
        #print startDay,endDay
        days  = myTimeMethod.get_slist_between_st_et(startDay, endDay)
        #print days
        
        last_day = (datetime.date(int(startDay[:4]),int(startDay[5:7]),int(startDay[8:10])) - datetime.timedelta(1)).strftime('%Y-%m-%d')
        last_datas = self.get_last_current_flow(group_devices, staPath=staPath, day=last_day)
        logging.info('last datas  get OK.length %s'%len(last_datas))
        for day in days:
            datas = {}
            logging.info('start sta %s flow'%day)
            sta_file = staPath + '/sta.%s.txt'%day
            if not os.path.exists(sta_file):
                logging.error('Sta File %s Not Exists.'%sta_file)
                continue
            #2015-06-08 18:34:31 Sta 9 [nioEventLoopGroup-5-4] INFO : 048d3861c7d8,buf={ "device_upflow": "31491682", "device_downflow": "71310327", "device_up_rate": 94, "device_down_rate": 203, "ntp_status": 1, "version": "V1.3.141231\n_2.1.5_20150520", "users": [ { "sta_name": "iPhone-2", "ssid": "Tangjiu", "sta_ip": "192.168.8.116", "sta_mac": "a0182884a6e9", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 39, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "15554", "sta_downflow": "41744" }, { "sta_name": "android-e975184", "ssid": "Tangjiu", "sta_ip": "192.168.8.149", "sta_mac": "88329b6b65fa", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 79, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "14757", "sta_downflow": "34698" }, { "sta_name": "applede-iPhone", "ssid": "Tangjiu", "sta_ip": "192.168.8.133", "sta_mac": "98f0ab648c74", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 99, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "54136", "sta_downflow": "238671" }, { "sta_name": "", "ssid": "Tangjiu", "sta_ip": "192.168.8.120", "sta_mac": "74e2f5568d8a", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 2192, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": "1844713", "sta_downflow": "14981302" }, { "sta_name": "android-2f005a9", "ssid": "Tangjiu", "sta_ip": "192.168.8.193", "sta_mac": "3c9157ec50dd", "sta_auth_type": 0, "sta_mode": 2, "sta_uptime": 4425, "sta_up_rate": 0, "sta_down_rate": 52, "sta_upflow": "1060806", "sta_downflow": "2855826" }, { "sta_name": "android-fb7719e", "ssid": "Tangjiu", "sta_ip": "192.168.8.196", "sta_mac": "18dc56b32a23", "sta_auth_type": 2, "sta_mode": 2, "sta_uptime": 6883, "sta_up_rate": 40, "sta_down_rate": 46, "sta_upflow": "3622895", "sta_downflow": "13890728" }, { "sta_name": "Pos0871", "ssid": "", "sta_ip": "192.168.8.166", "sta_mac": "00191700d40b", "sta_auth_type": 0, "sta_mode": 1, "sta_uptime": 59624, "sta_up_rate": 54, "sta_down_rate": 0, "sta_upflow": "12186735", "sta_downflow": "15587465" } ] }
            
            for line in open(sta_file).readlines():
                rtime , device , flow = self.analyze_flow_from_sta_log(line)
                if flow == None:continue
                hour = int(rtime[11:13])
                
                if device not in datas.keys():
                    datas[device] = {}
                if hour not in datas[device].keys():
                    datas[device][hour] = []
                datas[device][hour].append(flow)
            #print datas
            logging.info('%s datas OK.'%day)
            ddatas = {}
            for device , hourdatas in datas.items():
                ddatas[device] = {}
                #print device,
                if device in last_datas.keys():
                    last_current_flow = last_datas[device]
                else:
                    last_current_flow = 0
                for hour in range(24):
                    ddatas[device][hour] = 0
                    if hour not in hourdatas.keys():continue
                    hour_total_flow = 0
                    for current_flow in hourdatas[hour]:
                        if current_flow < last_current_flow:
                            hour_total_flow+=current_flow
                        else:
                            hour_total_flow+=(current_flow - last_current_flow)
                        last_current_flow = current_flow
                        last_datas[device] = last_current_flow
                    ddatas[device][hour] = hour_total_flow
                    #print '%s : %s'%(hour,hour_total_flow),
                #print ''
            #print last_datas[device],datas['048d3861cdc0']
            self.update_hour_flow(datas=ddatas, day=day)      
                    
    def update_hour_flow(self,datas={},day=None):
#         groupDevs = myRetailDatas.get_devices_by_group()
#         
#         devGroups = {}
#         for group,device in groupDevs.items():
#             devGroups[device] = group
        
        for device,hourDatas in datas.items():
#             if device in devGroups.keys():
#                 group = devGroups[device]
#             else:
#                 logging.error('device %s can not found any group.'%device)
#                 continue
            query = 'update device_flow_hour_stats set '
            if len(hourDatas)==0:
                logging.error('no hour flow in device %s.'%device)
                continue
            for hour,hour_flow in hourDatas.items():
                query += ' hour_%s="%s",'%(str(hour),hour_flow)
            query = re.sub(',$','',query)
            query += ' where deviceid="%s" and day="%s"'%(device,day)
            if self.testing==False:
                myZhiMofi.execute(query)
            
            
            logging.info('Hour Query: %s'%query)
            total_flow = sum(hourDatas.values())
            day_query = 'update device_day_rank_stats set flow="%s" where day="%s" and deviceid="%s"'%(total_flow,day,device)
            logging.info('Day Query : %s'%day_query)
            if self.testing==False:
                myZhiMofi.execute(day_query)
                
                
if __name__=='__main__':
    
    import argparse,re,datetime,time
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--staPath',metavar=u"sta path",default='/Udisk/mofidatas/')
    parser.add_argument('--startTime',metavar=u"day",default=None)
    parser.add_argument('--endTime',metavar=u"day",default=None)
    parser.add_argument('--testing',action='store_true',help='testing')
    
    args = parser.parse_args()  
    
    staPath = args.staPath
    startTime = args.startTime
    endTime = args.endTime
    testing = args.testing
    logging.basicConfig(level = logging.INFO)
    myStatInMofi = StatInMofi(testing=testing)
    #staPath = '/Users/chenyongbing/Downloads/sta/'
    myStatInMofi.stat_flow_by_hour_history('',staPath=staPath, startTime=startTime+' 0', endTime=endTime+ ' 23')
        
    #print myStatInMofi.get_last_current_flow('', staPath='/Users/chenyongbing/Downloads/sta/', day='2015-08-01')    
    